package com.example.controller;

import com.example.domain.entity.Material;
import com.example.domain.query.ProductQuery;
import com.example.service.IMaterialService;
import com.example.domain.query.MaterialQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import io.swagger.v3.oas.annotations.Operation;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/material")
@Tag(name = "物料管理器")
public class MaterialController {

    @Autowired
    private IMaterialService materialService;

    @GetMapping("/list")
    @Operation(summary = "获取物料列表")
    public List<Material> list() {
        return materialService.list();
    }

    @PostMapping("/save")
    @Operation(summary = "新增物料")
    public boolean save(@RequestBody Material material) {
        return materialService.save(material);
    }

    @PostMapping("/update")
    @Operation(summary = "更新物料")
    public boolean update(@RequestBody Material material) {
        return materialService.updateById(material);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除物料")
    public void deleteById(@PathVariable int id) {
        if (!materialService.removeById(id)) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "删除失败，物料不存在");
        }
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询物料")
    public IPage<Material> pageQuery(@RequestBody MaterialQuery query,
                                     @RequestParam(defaultValue = "1") int pageNum,
                                     @RequestParam(defaultValue = "10") int pageSize) {
        return materialService.pageQuery(query, pageNum, pageSize);
    }
}
